package com.machine.util;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * This file is used to import rh files into databases
 * 
 * @author Bird
 * 
 */
public class ImportModuleFiles {
	private static String url = "jdbc:mysql://localhost:3306/machine?characterEncoding=utf8";
	private static String user = "root";
	private static String password = "mysql";
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void insert(String name) throws Exception {
		
		Connection conn = null;
		Statement st = null;

		conn = DriverManager.getConnection(url, user, password);// 建立连接

		st = conn.createStatement();// 创建语句

		String t = "modelupload\\\\" + name+".rh";
		String sql = "insert into model(name,path) values('"+name+"','"+t+"')";
		System.out.println(sql);
		st.executeUpdate(sql);// 执行语句

	}

	public static void main(String[] args) throws Exception {
		String path = "D://Program Files//Apache Software Foundation//Tomcat 6.0//webapps//Machine//modelupload";
		File dir = new File(path);
		File files[] = dir.listFiles();
		for (int i = 0; i < files.length; i++) {
			String tempName = files[i].getName();
			tempName = tempName.substring(0, tempName.indexOf((".rh")));
			insert(tempName);
		}
		//insert("123");
	}
}
